function show_tool(obj_name){
    var objName = document.getElementById(obj_name);

    if(objName.style.display == "none"){
      //objName.style.position = "absolute";
      objName.style.display = "block";
      //objName.style.zIndex = 10000;
      objName.style.visibility  = "visible";
    } else {
      objName.style.display = "none";
    }

}

function show_submenu(inhtml){
    var objName = document.getElementById('submenu');
    objName.innerHTML = inhtml;

}
function addOrders(id, desc, price) {

  var ni = document.getElementById('orders');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;

    for (i = 1; i < num; i++){
        var prod_id = document.getElementById('prod_id'+i);
        var quantity = document.getElementById('quantity'+i);
        if (prod_id) {
            if (prod_id.value == id){
                quantity.value = (quantity.value - 1) + 2;
                getTotal();
                return false;
            }
        }
    }

  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  var alt = (Math.ceil(num/2) == num/2) ? 'alt' : 'alt1';
  newdiv.setAttribute('id',divIdName);

  var html =  '<table width="99%" border="0" cellspacing="0" cellpadding="5"><tr>' +
                '<td width="25%" align="left" class="'+alt+'">' + desc.toLowerCase() + '</td>' +
                '<td width="25%" align="center" class="'+alt+'">' + price + '</td>' +
                '<td width="25%" align="center" class="'+alt+'">' +
                    '<input name="quantity[]" id="quantity'+num+'" value="1" onkeyup="getTotal()" size=5 class="transtext">' +
                    '<input name="prod_id[]" id="prod_id'+num+'" type="hidden" value="' + id + '">' +
                    '<input name="price[]" id="price'+num+'" type="hidden" value="' + price + '">' +
                '</td>' +
                '<td width="25%" align="center" class="'+alt+'"><a class="remove" onclick="removeOrders(\'' +divIdName+ '\')">Remove</a></td>'+
              '</tr></table>';

  newdiv.innerHTML = html;
  ni.appendChild(newdiv);

  getTotal();
}

function removeOrders(divNum) {
  var d = document.getElementById('orders');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
  getTotal();
}


function getTotal(){

    var count = (document.getElementById('theValue').value -1)+ 2;
    var gtotal = document.getElementById('total');
    var vat = document.getElementById('vat');
    var amount_due = document.getElementById('amount_due');
    var sum = 0;
    for (i = 1; i < count; i++){
        var price = document.getElementById('price'+i);
        var quantity = document.getElementById('quantity'+i);
        if (quantity) {
            if (checknum(quantity.value) == false){
                alert('Please input a valid number');
                quantity.value = 1;
            }
            sum  += Math.round(quantity.value) * Math.abs(price.value);
        }
    }
        gtotal.innerHTML = Math.round(sum * 0.88 * 100)/100;
        vat.innerHTML = Math.round((sum * 0.12)*100)/100;
        amount_due.innerHTML = Math.round(sum * 100)/100;

}

function getChange(){
    var amount_due = document.getElementById('amount_due');
    var change = document.getElementById('change');
    var cash = document.getElementById('cash');

    if (checknum(cash.value) == false){
        alert('Please input a valid number');
        cash.value = '';
    }
    change.innerHTML = Math.round((cash.value - amount_due.innerHTML)*100)/100;
    return change.innerHTML

}
function checknum(nump){
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;
    for (ix = 0; ix < nump.length && IsNumber == true; ix++)   {
        Char = nump.charAt(ix);
        if (ValidChars.indexOf(Char) == -1)  {
            IsNumber = false;
            return false;
        }
    }

    return true;
}

function removeElem(elem){

    var el = document.getElementById(elem);
    el.parentNode.removeChild(el);

}

